/**
 * 
 */
package com.nari.baseinfo.utils.excelupload;

import com.nari.baseinfo.utils.excelupload.parseentity.ExcelAssignMeterEntity;
import com.nari.baseinfo.utils.excelupload.parseentity.ExcelHeatSinkerEntity;
import com.nari.baseinfo.utils.excelupload.parseentity.ExcelRecordEntity;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.utils.NumberSystemConversion;

/**
 * 基础资料信息校验
 * @author Xiaopeng
 * @date 2013-12-23
 */
public class BaseInfoExcelVerify {

	/**
	 * 校验Excel记录信息，如基础信息的必填项、修正数据的数据范围等
	 * @param excelRecord
	 * @return
	 * @author Xiaopeng
	 * @date 2013-12-23
	 */
	public static ResponseMsg verifyExcelRecord(ExcelRecordEntity excelRecord) {
		//校验反馈信息
		StringBuffer verifyMsg = new StringBuffer("");
		//是否参与分摊
		if(excelRecord.getIfMeasureCharge() == null) {
			verifyMsg.append("请检查Excel中行号为【");
			verifyMsg.append(excelRecord.getRowCounter());
			verifyMsg.append("】的以下列是否填写完整：");
			verifyMsg.append("<是否参与分摊>");
			verifyMsg.append("(内容请填写【参与】或【不参与】)");
			return ResponseMsg.getInstance(false, verifyMsg.toString());
		}
		//不参与计量收费的用户信息校验
		if(excelRecord.getIfMeasureCharge() == 1) {
			return verifyAreaChargeRecord(excelRecord);
		}
		//参与计量收费的用户信息校验
		if(excelRecord.getIfMeasureCharge() == 2) {
			return verifyMeasureChargeRecord(excelRecord);
		}
		return ResponseMsg.getInstance(true, "");
	}
	
	/**
	 * 校验参与计量收费用户信息完整性
	 * @param excelRecord
	 * @return
	 * @author Xiaopeng
	 * @date 2014-1-12
	 */
	private static ResponseMsg verifyMeasureChargeRecord(ExcelRecordEntity excelRecord) {
		//校验标识位
		boolean verifyFlag = true;
		//校验反馈信息
		StringBuffer verifyMsg = new StringBuffer("");
		//记录ID标识
		if(excelRecord.getRecordID() == null) {
			verifyFlag = false;
			verifyMsg.append("<ID>").append("、");
		}
		//楼栋编号
		if(excelRecord.getBuildingID()==null || "".equals(excelRecord.getBuildingID().trim())) {
			verifyFlag = false;
			verifyMsg.append("<楼栋编号>").append("、");
		}
		//单元
		if(excelRecord.getUnitName()==null || "".equals(excelRecord.getUnitName().trim())) {
			verifyFlag = false;
			verifyMsg.append("<单元号>").append("、");
		}
		//层号
		if(excelRecord.getFloorIndex()==null || "".equals(excelRecord.getFloorIndex().trim())) {
			verifyFlag = false;
			verifyMsg.append("<层号>").append("、");
		}
		//房间号
		if(excelRecord.getHouseNo()==null || "".equals(excelRecord.getHouseNo().trim())) {
			verifyFlag = false;
			verifyMsg.append("<房间号>").append("、");
		}
		//建筑面积
		if(NumberSystemConversion.compareFloatValue(excelRecord.getStructArea(), 0) <= 0) {
			verifyFlag = false;
			verifyMsg.append("<建筑面积>").append("、");
		}
		//户修正系数
		if(NumberSystemConversion.compareFloatValue(excelRecord.getReviseFactor(), 0) < 0) {
			verifyFlag = false;
			verifyMsg.append("<户修正系数>").append("、");
		}
		//分配表表号校验
		ExcelAssignMeterEntity excelAssignMeter = null;
		if(excelRecord.getAssignMeterList().size() > 0) {
			excelAssignMeter = excelRecord.getAssignMeterList().get(0);
		}
		String seriousNum = null;
		if(excelAssignMeter != null) {
			seriousNum = excelAssignMeter.getSeriousNum();
		}
		if(seriousNum==null || "".equals(seriousNum.trim()) 
				|| excelAssignMeter.getSeriousNum().length()<=3) {
			verifyFlag = false;
			verifyMsg.append("<分配表表号>").append("、");
		}
		ExcelHeatSinkerEntity excelHeatSinker = excelRecord.getHeatSinkerList().get(0);
		//散热器型号
		if(excelHeatSinker.getSinkType()==null || "".equals(excelHeatSinker.getSinkType().trim())) {
			verifyFlag = false;
			verifyMsg.append("<散热器型号>").append("、");
		}
		//中心矩
		if(NumberSystemConversion.compareFloatValue(excelHeatSinker.getSinkCenDis(), 0) <= 0) {
			verifyFlag = false;
			verifyMsg.append("<中心矩>").append("、");
		}
		//片数
		if(excelHeatSinker.getPieceNum() <= 0) {
			verifyFlag = false;
			verifyMsg.append("<片数>").append("、");
		}
		//单片功率
		if(NumberSystemConversion.compareFloatValue(excelHeatSinker.getCalPower(), 0) <= 0) {
			verifyFlag = false;
			verifyMsg.append("<单片功率>").append("、");
		}
		//温控方式
		if(excelHeatSinker.getTmpControlMode()==null || "".equals(excelHeatSinker.getTmpControlMode().trim())) {
			verifyFlag = false;
			verifyMsg.append("<温控方式>").append("、");
		}
		//是否有罩
		if(excelHeatSinker.getIsHasHat()==null || "".equals(excelHeatSinker.getIsHasHat().trim())) {
			verifyFlag = false;
			verifyMsg.append("<是否有罩>").append("、");
		}
		//低温修正系数Kt
		if(NumberSystemConversion.compareFloatValue(excelHeatSinker.getKtFactor(), 0) < 0) {
			verifyFlag = false;
			verifyMsg.append("<KT>").append("、");
		}
		//耦合修正系数Kc
		if(NumberSystemConversion.compareFloatValue(excelHeatSinker.getKcFactor(), 0) < 0) {
			verifyFlag = false;
			verifyMsg.append("<KC>").append("、");
		}
		//构造校验结果
		if(verifyFlag) {
			return ResponseMsg.getInstance(true, "");
		} 
		String rmsg = "请检查Excel中行号为【"+excelRecord.getRowCounter()+"】的以下列是否填写完整：";
		rmsg += verifyMsg.substring(0, verifyMsg.length()-1);
		return ResponseMsg.getInstance(false, rmsg);
	}
	
	/**
	 * 校验不参与计量收费用户信息完整性
	 * @param excelRecord
	 * @return
	 * @author Xiaopeng
	 * @date 2014-1-12
	 */
	private static ResponseMsg verifyAreaChargeRecord(ExcelRecordEntity excelRecord) {
		//校验标识位
		boolean verifyFlag = true;
		//校验反馈信息
		StringBuffer verifyMsg = new StringBuffer("");
		//记录ID标识
		if(excelRecord.getRecordID() == null) {
			verifyFlag = false;
			verifyMsg.append("<ID>").append("、");
		}
		//楼栋编号
		if(excelRecord.getBuildingID()==null || "".equals(excelRecord.getBuildingID().trim())) {
			verifyFlag = false;
			verifyMsg.append("<楼栋编号>").append("、");
		}
		//单元
		if(excelRecord.getUnitName()==null || "".equals(excelRecord.getUnitName().trim())) {
			verifyFlag = false;
			verifyMsg.append("<单元号>").append("、");
		}
		//层号
		if(excelRecord.getFloorIndex()==null || "".equals(excelRecord.getFloorIndex().trim())) {
			verifyFlag = false;
			verifyMsg.append("<层号>").append("、");
		}
		//房间号
		if(excelRecord.getHouseNo()==null || "".equals(excelRecord.getHouseNo().trim())) {
			verifyFlag = false;
			verifyMsg.append("<房间号>").append("、");
		}
		//建筑面积
		if(NumberSystemConversion.compareFloatValue(excelRecord.getStructArea(), 0) <= 0) {
			verifyFlag = false;
			verifyMsg.append("<建筑面积>").append("、");
		}
		/** 校验不参与分摊热用户是否安装分配表；
		 * 若安装分配表，则需要校验分配表和散热器信息
		 *  **/
		//分配表表号校验
		ExcelAssignMeterEntity excelAssignMeter = null;
		if(excelRecord.getAssignMeterList().size() > 0) {
			excelAssignMeter = excelRecord.getAssignMeterList().get(0);
		}
		String seriousNum = null;
		if(excelAssignMeter != null) {
			seriousNum = excelAssignMeter.getSeriousNum();
		}
		if(seriousNum!=null && !"".equals(seriousNum.trim())) {
			//分配表表号一般规则：集中器号(12位) + 分配表序号(3位)
			if(excelAssignMeter.getSeriousNum().length() <= 3) {
				verifyFlag = false;
				verifyMsg.append("<分配表表号>").append("、");
			}
			ExcelHeatSinkerEntity excelHeatSinker = excelRecord.getHeatSinkerList().get(0);
			//散热器型号
			if(excelHeatSinker.getSinkType()==null || "".equals(excelHeatSinker.getSinkType().trim())) {
				verifyFlag = false;
				verifyMsg.append("<散热器型号>").append("、");
			}
			//中心矩
			if(NumberSystemConversion.compareFloatValue(excelHeatSinker.getSinkCenDis(), 0) <= 0) {
				verifyFlag = false;
				verifyMsg.append("<中心矩>").append("、");
			}
			//片数
			if(excelHeatSinker.getPieceNum() <= 0) {
				verifyFlag = false;
				verifyMsg.append("<片数>").append("、");
			}
			//单片功率
			if(NumberSystemConversion.compareFloatValue(excelHeatSinker.getCalPower(), 0) <= 0) {
				verifyFlag = false;
				verifyMsg.append("<单片功率>").append("、");
			}
			//温控方式
			if(excelHeatSinker.getTmpControlMode()==null || "".equals(excelHeatSinker.getTmpControlMode().trim())) {
				verifyFlag = false;
				verifyMsg.append("<温控方式>").append("、");
			}
			//是否有罩
			if(excelHeatSinker.getIsHasHat()==null || "".equals(excelHeatSinker.getIsHasHat().trim())) {
				verifyFlag = false;
				verifyMsg.append("<是否有罩>").append("、");
			}
			//低温修正系数Kt
			if(NumberSystemConversion.compareFloatValue(excelHeatSinker.getKtFactor(), 0) < 0) {
				verifyFlag = false;
				verifyMsg.append("<KT>").append("、");
			}
			//耦合修正系数Kc
			if(NumberSystemConversion.compareFloatValue(excelHeatSinker.getKcFactor(), 0) < 0) {
				verifyFlag = false;
				verifyMsg.append("<KC>").append("、");
			}
		}
		//构造校验结果
		if(verifyFlag) {
			return ResponseMsg.getInstance(true, "");
		} 
		String rmsg = "请检查Excel中行号为【"+excelRecord.getRowCounter()+"】的以下列是否填写完整：";
		rmsg += verifyMsg.substring(0, verifyMsg.length()-1);
		return ResponseMsg.getInstance(false, rmsg);
	}
	
	/**
	 * 检查同一热用户的基本信息是否一致
	 * @param referExcelRecord
	 * @param excelRecord
	 * @return
	 * @author Xiaopeng
	 * @date 2013-12-23
	 */
	public static ResponseMsg verifyUserInfoUniform(ExcelRecordEntity referExcelRecord, 
			ExcelRecordEntity excelRecord) {
		//校验标识位
		boolean verifyFlag = true;
		//校验反馈信息
		StringBuffer verifyMsg = new StringBuffer("");
		//用户姓名
		if(excelRecord.getUserName() != null) {
			if(!excelRecord.getUserName().equals(referExcelRecord.getUserName())) {
				verifyFlag = false;
				verifyMsg.append("<用户姓名>").append("、");
			}
		}
		if(excelRecord.getUserName()==null && referExcelRecord.getUserName()!=null) {
			verifyFlag = false;
			verifyMsg.append("<用户姓名>").append("、");
		}
		//联系电话
		if(excelRecord.getPhone() != null) {
			if(!excelRecord.getPhone().equals(referExcelRecord.getPhone())) {
				verifyFlag = false;
				verifyMsg.append("<联系电话>").append("、");
			}
		}
		if(excelRecord.getPhone()==null && referExcelRecord.getPhone()!=null) {
			verifyFlag = false;
			verifyMsg.append("<联系电话>").append("、");
		}
		//楼栋编号
		if(!excelRecord.getBuildingID().equals(referExcelRecord.getBuildingID())) {
			verifyFlag = false;
			verifyMsg.append("<楼栋编号>").append("、");
		}
		//单元号
		if(!excelRecord.getUnitName().equals(referExcelRecord.getUnitName())) {
			verifyFlag = false;
			verifyMsg.append("<单元号>").append("、");
		}
		//层号
		if(!excelRecord.getFloorIndex().equals(referExcelRecord.getFloorIndex())) {
			verifyFlag = false;
			verifyMsg.append("<层号>").append("、");
		}
		//房间号
		if(!excelRecord.getHouseNo().equals(referExcelRecord.getHouseNo())) {
			verifyFlag = false;
			verifyMsg.append("<房间号>").append("、");
		}
		//是否参与分摊
		if(excelRecord.getIfMeasureCharge().intValue() != referExcelRecord.getIfMeasureCharge()) {
			verifyFlag = false;
			verifyMsg.append("<是否参与分摊>").append("、");
		}
		//建筑面积
		if(NumberSystemConversion.compareFloatValue(excelRecord.getStructArea(), referExcelRecord.getStructArea()) != 0) {
			verifyFlag = false;
			verifyMsg.append("<建筑面积>").append("、");
		}
		//使用面积
		if(NumberSystemConversion.compareFloatValue(excelRecord.getHeatArea(), referExcelRecord.getHeatArea()) != 0) {
			verifyFlag = false;
			verifyMsg.append("<使用面积>").append("、");
		}
		//户修正系数
		if(NumberSystemConversion.compareFloatValue(excelRecord.getReviseFactor(), referExcelRecord.getReviseFactor()) != 0) {
			verifyFlag = false;
			verifyMsg.append("<户修正系数>").append("、");
		}
		//楼层位置
		if(excelRecord.getFloorPosition() != null) {
			if(!excelRecord.getFloorPosition().equals(referExcelRecord.getFloorPosition())) {
				verifyFlag = false;
				verifyMsg.append("<楼层位置>").append("、");
			}
		}
		if(excelRecord.getFloorPosition()==null && referExcelRecord.getFloorPosition()!=null) {
			verifyFlag = false;
			verifyMsg.append("<楼层位置>").append("、");
		}
		//是否为边户
		if(excelRecord.getIfSideRoom() != null) {
			if(!excelRecord.getIfSideRoom().equals(referExcelRecord.getIfSideRoom())) {
				verifyFlag = false;
				verifyMsg.append("<是否为边户>").append("、");
			}
		}
		if(excelRecord.getIfSideRoom()==null && referExcelRecord.getIfSideRoom()!=null) {
			verifyFlag = false;
			verifyMsg.append("<是否为边户>").append("、");
		}
		//构造校验结果
		if(verifyFlag) {
			return ResponseMsg.getInstance(true, "");
		} 
		String rmsg = "请检查Excel中行号为【"+excelRecord.getRowCounter()+"】的以下列是否与" +
				"ID为【"+referExcelRecord.getRecordID()+"】的行的对应列一致：";
		rmsg += verifyMsg.substring(0, verifyMsg.length()-1);
		return ResponseMsg.getInstance(false, rmsg);
	}
}
